#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/3/21 18:50
# @Author  : huidong.bai
# @File    : deploy_environment.py
# @Software: PyCharm
# @Mail    : MasterBai2018@outlook.com

import subprocess
import os
import pdb


def get_work_mode(cmdArgs):
    if cmdArgs.mongo_solution == "null" and cmdArgs.mongo_filter == "null":
        print("You must enter solution or specify one suite when run case,the more details in help.")
        return 'INVALID'
    elif cmdArgs.mongo_solution != "null" and cmdArgs.mongo_filter != "null":
        if cmdArgs.mongo_filer_config != "null":
            print("Only process the suite that specified by '--mongo_filter' when '--mongo_solution' and "
                  "'--mongo_filer' exist together.")
            if cmdArgs.mongo_gdb:
                return 'GDB_FILTER'
            else:
                return 'FILTER'
        else:
            return 'OVERALL'
    elif cmdArgs.mongo_solution == "null":
        if cmdArgs.mongo_filter_config != "null":
            if cmdArgs.mongo_gdb:
                return 'GDB_FILTER'
            else:
                return 'FILTER'
        else:
            print("You must enter solution for the suite that specified by '--mongo_filter'.")
            return 'INVALID'
    else:
        return 'OVERALL'


def resource_deploy(branch, root_dir):
    if branch == 'null':
        return
    res_branch = branch.split('-')[0]
    case_branch = branch.split('-')[1]
    if not os.path.exists("TestResource"):
        try:
            subprocess.check_call("git clone git@192.168.129.110:asr/TestResource.git", shell=True)
            print("clone TestResource success")
        except subprocess.CalledProcessError as exc:
            print("clone TestResource failed")
            print('returncode:', exc.returncode)
            print('cmd:', exc.cmd)
            print('output:', exc.output)

    os.chdir("TestResource")
    subprocess.call("git fetch --all", shell=True)

    try:
        subprocess.check_call("git checkout .", shell=True)
        subprocess.check_call("git checkout %s " % res_branch, shell=True)
        print("git checkout %s success " % res_branch)
        subprocess.check_call("git pull", shell=True)
    except subprocess.CalledProcessError as exc:
        print("git checkout %s failed " % res_branch)
        print('returncode:', exc.returncode)
        print('cmd:', exc.cmd)
        print('output:', exc.output)

        subprocess.check_call("python2 generate_resource.py lexus_resource.script", shell=True)

    os.chdir(root_dir)

    if not os.path.exists("TestCase"):
        try:
            subprocess.check_call("git clone git@192.168.129.110:asr/TestCase.git", shell=True)
            print("clone TestCase success")
        except subprocess.CalledProcessError as exc:
            print("clone TestCase failed")
            print('returncode:', exc.returncode)
            print('cmd:', exc.cmd)
            print('output:', exc.output)

    os.chdir("TestCase")
    subprocess.call("git fetch --all", shell=True)

    try:
        subprocess.check_call("git checkout .", shell=True)
        subprocess.check_call("git checkout %s " % case_branch, shell=True)
        print("git checkout %s success " % case_branch)
        subprocess.check_call("git pull", shell=True)
    except subprocess.CalledProcessError as exc:
        print("git checkout %s failed " % case_branch)
        print('returncode:', exc.returncode)
        print('cmd:', exc.cmd)
        print('output:', exc.output)

    os.chdir(root_dir)
